var express = require('express')
var router = express.Router()
var { FindOneDataMore, InsertManyData, UpdateTableData, FindTableData, RemoveTableData } = require('../db')
const { advise_model} = require('../db/model')
var axios = require("axios")
const { getToken, checkToken } = require('../utils/token')
var multer = require("multer")   //上传文件

//意见新增
router.post("/adviseAdd",(req,res)=>{
    var body =req.body
    body.time = new Date()
    checkToken(req,res,({phone})=>{
        InsertManyData({
            model:advise_model,
            data:body,
            res,
            msg:"成功"
        })
    })
})
// 意见列表 
router.post("/adviseList",(req,res)=>{
    var body = req.body; 
    var query = {} 
    var {keyword,type} = body
    if(keyword && type){
        query = {
            title:new RegExp(keyword),
            type:type  * 1 
        }
    }else if(keyword){
        query = {
            $or:[
                {
                    title:new RegExp(keyword),
                },
                {
                    content:new RegExp(keyword),
                }
            ]
        }
    }else if(type){
        query = {
            type:type * 1 
        }
    }
    checkToken(req,res,({phone})=>{
        FindTableData({
            model:advise_model,
            query:query,
            msg:'成功',
            res 
        })
    })
})
// 意见删除
router.post("/adviseDelete",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({phone})=>{
        RemoveTableData({
            model:advise_model,
            query:{
                _id:body._id
            },
            msg:'成功',
            res 
        })
    })
})

// 查询一个意见
router.post("/adviseOne",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({phone})=>{
        FindOneDataMore({
            model:advise_model,
            query:{
                _id:body._id
            },
            msg:'成功',
            res 
        })
    })
})

// 修改意见
router.post("/adviseUpdate",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({phone})=>{
        UpdateTableData({
            model:advise_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'成功',
            res 
        })
    })
})
module.exports = router;